﻿ ////////////////////////////////////////////////////////////////////////////////
//
// NetControl Track Network Master
//
////////////////////////////////////////////////////////////////////////////////
package com.uprr.app.tnm.events
{
	import flash.events.EventDispatcher;
	import flash.events.Event;
	import mx.controls.Alert;
	import mx.utils.ObjectUtil;
	/**
	* The EventBus allows centrallized communication among modules without
	* point-to-point messaging. It uses the singleton design pattern
	* to make sure one event bus is available globally. The bus itself
	* is only available to the container. Modules use the container's
	* static method to communicate with the event bus.
	*/
	public class EventBus extends EventDispatcher
	{
		/** Application event buss instance */
		private static var _eventBus : EventBus;
		/** Lock to enforce singleton */
		private static var lock : Boolean = false;
		/**
		* Normally the EventBus is not instantiated via the <b>new</b> method directly.
		* The constructor helps enforce only one EvenBus availiable for the application
		* (singeton) so that it asures the communication only via a sigle event bus.
		*/
		public function EventBus ()
		{
			if ( ! lock)
			throw new Error ("ContainerEventDispatcher can only be defined once!");
			initialize ();
		}
		/**
		* The factory method is used to create a instance of the EventBus. It returns
		* the only instanace of EventBus and makes sure no another instance is created.
		*/
		public static function getInstance () : EventBus
		{
			if (_eventBus == null)
			{
				lock = true;
				_eventBus = new EventBus ();
				lock = false;
			}
			return _eventBus;
		}
		/**
		* This is place holder for future features implementation.
		*/
		private function initialize () : void
		{
			// todo: some initialization...
			
		}
		/**
		* Basic dispatch function, dispatches simple named events. In the case
		* that the event is only significant by the event token (type string),
		* this new dispatch method simplify the code.
		*/
		private function dispatch (type : String) : Boolean
		{
			return dispatchEvent (new Event (type));
		}
		/**
		*/
		public static function addEventListener (type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void
		{
			//Alert.show(type+" listener is added");
			EventBus.getInstance ().addEventListener (type, listener, useCapture, priority, useWeakReference);
		}
		
		
		public static function removeEventListener (type : String, listener : Function, useCapture : Boolean = false) : void
		{
			EventBus.getInstance ().removeEventListener (type, listener, useCapture);
		}
		
		//this is the simplest way of communicate. Just use a string.
		public static function dispatch (type : String) : Boolean
		{
			return EventBus.getInstance ().dispatch (type);
		}
		
		
		public static function dispatchEvent (event : Event) : Boolean
		{
			//Alert.show(event.type+" event is dispatched");
			return EventBus.getInstance ().dispatchEvent (event);
		}
		
		
		/**
		*
		*/
		public static function setStatus (status : String) : void
		{
			EventBus.getInstance().dispatchEvent (new AppEvent (AppEvent.SET_STATUS, false, false, status));
		}
		
		
		
		
		
	}
}
